package com.xx.sg.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xx.sg.own.entity.CommandRecord;
import com.xx.sg.own.service.CommandRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/commandRecord")
@Tag(name = "指挥档案", description = "指挥档案API")
public class CommandRecordController {

    @Autowired
    private CommandRecordService commandRecordService;

    @GetMapping("/info/{id}")
    @Operation(summary = "档案列表", description = "档案列表")
    public Map<String, Object> info(@PathVariable("id") String id) {
        CommandRecord commandRecord = commandRecordService.getById(id);
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("data", commandRecord);
        res.put("message", "成功");
        return res;
    }


    @GetMapping("/list")
    @Operation(summary = "档案列表", description = "档案列表")
    public Map list(@ModelAttribute CommandRecord commandRecord) {
        LambdaQueryWrapper<CommandRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(commandRecord.getCid()), CommandRecord::getCid, commandRecord.getCid())
                .orderByDesc(CommandRecord::getCreateTime);
        List<CommandRecord> list = commandRecordService.list(wrapper);
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("data", list);
        res.put("message", "成功");
        return res;
    }

    @PostMapping("/add")
    @Operation(summary = "新增档案", description = "新增档案")
    public Map add(@RequestBody CommandRecord commandRecord) {
        commandRecordService.save(commandRecord);
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("data", true);
        res.put("message", "成功");
        return res;
    }

}
